#!/bin/bash

################################################################################
# 脚本名称: 2-import-to-nexus.sh
# 执行位置: 86 Nexus服务器
# 功能描述: 导入tar镜像 → 打标签 → 推送到Nexus私有仓库
# 作者: 高宇
################################################################################

set -e

# ============================================================================
# 配置参数
# ============================================================================

# 镜像信息
IMAGE_NAME="hydro-model-base"
IMAGE_TAG="openjdk17"
IMAGE_VERSION="1.0.0"

# Nexus仓库信息
NEXUS_HOST="172.20.7.86:5001"
NEXUS_USERNAME="admin"
NEXUS_PASSWORD="admin123"
NEXUS_REPO="hydro"

# tar文件路径（当前目录）
EXPORT_FILE="${IMAGE_NAME}-${IMAGE_TAG}.tar"

# ============================================================================
# 颜色输出
# ============================================================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# ============================================================================
# 步骤1: 导入镜像
# ============================================================================
import_image() {
    print_info "=========================================="
    print_info "步骤1: 导入Docker镜像"
    print_info "=========================================="
    
    if [ ! -f "${EXPORT_FILE}" ]; then
        print_error "文件不存在: ${EXPORT_FILE}"
        exit 1
    fi
    
    print_info "导入镜像: ${EXPORT_FILE}"
    docker load -i ${EXPORT_FILE}
    
    print_success "镜像导入完成！"
    docker images | grep ${IMAGE_NAME}
}

# ============================================================================
# 步骤2: 打Nexus标签
# ============================================================================
tag_image() {
    print_info "=========================================="
    print_info "步骤2: 打Nexus标签"
    print_info "=========================================="
    
    print_info "打标签: ${IMAGE_TAG}"
    docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${NEXUS_HOST}/${NEXUS_REPO}/${IMAGE_NAME}:${IMAGE_TAG}
    
    print_info "打标签: ${IMAGE_VERSION}"
    docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${NEXUS_HOST}/${NEXUS_REPO}/${IMAGE_NAME}:${IMAGE_VERSION}
    
    print_info "打标签: latest"
    docker tag ${IMAGE_NAME}:${IMAGE_TAG} ${NEXUS_HOST}/${NEXUS_REPO}/${IMAGE_NAME}:latest
    
    print_success "标签创建完成！"
    docker images | grep ${IMAGE_NAME}
}

# ============================================================================
# 步骤3: 登录Nexus
# ============================================================================
login_nexus() {
    print_info "=========================================="
    print_info "步骤3: 登录Nexus仓库"
    print_info "=========================================="
    
    print_info "登录: ${NEXUS_HOST}"
    echo "${NEXUS_PASSWORD}" | docker login ${NEXUS_HOST} -u ${NEXUS_USERNAME} --password-stdin
    
    print_success "登录成功！"
}

# ============================================================================
# 步骤4: 推送镜像到Nexus
# ============================================================================
push_image() {
    print_info "=========================================="
    print_info "步骤4: 推送镜像到Nexus"
    print_info "=========================================="
    
    print_info "推送: ${IMAGE_TAG}"
    docker push ${NEXUS_HOST}/${NEXUS_REPO}/${IMAGE_NAME}:${IMAGE_TAG}
    
    print_info "推送: ${IMAGE_VERSION}"
    docker push ${NEXUS_HOST}/${NEXUS_REPO}/${IMAGE_NAME}:${IMAGE_VERSION}
    
    print_info "推送: latest"
    docker push ${NEXUS_HOST}/${NEXUS_REPO}/${IMAGE_NAME}:latest
    
    print_success "镜像推送完成！"
}

# ============================================================================
# 步骤5: 验证Nexus中的镜像
# ============================================================================
verify_nexus() {
    print_info "=========================================="
    print_info "步骤5: 验证Nexus中的镜像"
    print_info "=========================================="
    
    print_info "检查镜像仓库目录..."
    curl -s -u ${NEXUS_USERNAME}:${NEXUS_PASSWORD} \
        -X GET "http://${NEXUS_HOST}/v2/_catalog" | python3 -m json.tool || true
    
    echo ""
    print_info "检查镜像标签列表..."
    curl -s -u ${NEXUS_USERNAME}:${NEXUS_PASSWORD} \
        -X GET "http://${NEXUS_HOST}/v2/${NEXUS_REPO}/${IMAGE_NAME}/tags/list" | python3 -m json.tool || true
    
    print_success "验证完成！"
}

# ============================================================================
# 步骤6: 清理本地文件
# ============================================================================
cleanup() {
    print_info "=========================================="
    print_info "步骤6: 清理本地文件"
    print_info "=========================================="
    
    print_info "删除tar文件: ${EXPORT_FILE}"
    rm -f ${EXPORT_FILE}
    
    print_info "清理本地镜像标签（保留基础镜像）..."
    docker rmi ${NEXUS_HOST}/${NEXUS_REPO}/${IMAGE_NAME}:${IMAGE_TAG} || true
    docker rmi ${NEXUS_HOST}/${NEXUS_REPO}/${IMAGE_NAME}:${IMAGE_VERSION} || true
    docker rmi ${NEXUS_HOST}/${NEXUS_REPO}/${IMAGE_NAME}:latest || true
    
    print_success "清理完成！"
}

# ============================================================================
# 主流程
# ============================================================================
main() {
    echo ""
    echo "╔════════════════════════════════════════════════════════════════╗"
    echo "║                                                                ║"
    echo "║      [脚本2] 86服务器 - 导入镜像并推送到Nexus                   ║"
    echo "║                                                                ║"
    echo "╚════════════════════════════════════════════════════════════════╝"
    echo ""
    
    print_info "镜像名称: ${IMAGE_NAME}:${IMAGE_TAG}"
    print_info "Nexus地址: ${NEXUS_HOST}/${NEXUS_REPO}/${IMAGE_NAME}"
    echo ""
    
    # 执行所有步骤
    import_image
    tag_image
    login_nexus
    push_image
    verify_nexus
    cleanup
    
    echo ""
    print_success "╔════════════════════════════════════════════════════════════════╗"
    print_success "║                    所有步骤执行完成！                            ║"
    print_success "╚════════════════════════════════════════════════════════════════╝"
    echo ""
    
    print_info "基础镜像已推送到Nexus，可以用于构建服务镜像了！"
    print_info "镜像地址: ${NEXUS_HOST}/${NEXUS_REPO}/${IMAGE_NAME}:latest"
    echo ""
}

# 执行主流程
main

